<?php


namespace app\common\repositories\system\merchant;


use app\common\dao\system\merchant\MerchantRuleDao;
use app\common\repositories\BaseRepository;
use FormBuilder\Exception\FormBuilderException;
use FormBuilder\Factory\Elm;
use FormBuilder\Form;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Route;


class MerchantRuleRepository extends BaseRepository
{
    /**
     * MerchantRuleRepository constructor.
     * @param MerchantRuleRepository $dao
     */
    public function __construct(MerchantRuleDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * @param array $where
     * @param $page
     * @param $limit
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function search( array $where, $page, $limit)
    {
        $query = $this->dao->search($where)->order('create_time DESC');
        $count = $query->count($this->dao->getPk());

        $list = $query->page($page, $limit)->hidden(['update_time'])->select();
        return compact('count', 'list');
    }




    /**
     * @param $id
     * @return Form
     * @throws FormBuilderException
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function updateForm($id)
    {
        return $this->form($id, $this->dao->get($id)->toArray());
    }


    public function merExists(int $id, $except = null)
    {
        return $this->dao->merFieldExists($this->dao->getPk(), $id, $except);
    }

    /**
     * @param array $where
     * @param $page
     * @param $limit
     * @param $orderBy
     * @param $field
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author: fupeng
     * @day: 2023/11/29
     */
    public function getApiList(array $where, $page, $limit, $orderBy = null ,$field ='*')
    {
        $query = $this->dao->search($where);
        $count = $query->count();
        $query = $query->page($page, $limit)->setOption('field', [])->field($field);
        if($orderBy){
            $query->order($orderBy);
        }
        $list = $query->select();
        return compact('count', 'list');
    }
}
